%
% Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
% Use is subject to license terms.
%
% CDDL HEADER START
%
% The contents of this file are subject to the terms of the
% Common Development and Distribution License, Version 1.0 only
% (the "License").  You may not use this file except in compliance
% with the License.
%
% You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
% or http://www.opensolaris.org/os/licensing.
% See the License for the specific language governing permissions
% and limitations under the License.
%
% When distributing Covered Code, include this CDDL HEADER in each
% file and include the License file at usr/src/OPENSOLARIS.LICENSE.
% If applicable, add the following below this CDDL HEADER, with the
% fields enclosed by brackets "[]" replaced with your own identifying
% information: Portions Copyright [yyyy] [name of copyright owner]
%
% CDDL HEADER END
%
% Use is subject to license terms
%
%ident	"%Z%%M%	%I%	%E% SMI"
%

/add-euro-to-font {
	/new-font exch def
	/old-font exch def

	% Begin Euro Font: A single character "Euro" font
	10 dict begin
		/FontType 3 def
		/FontMatrix [.00060 0 0 .00060 0 0] def
		/FontBBox [0 0 1000 1000] def

		/Encoding [ /Euro ] def

		/CharProcs 2 dict def
		CharProcs begin
			/.notdef { } def
			/Euro {			% Draw an EUR glyph
  				gsave
  				955.852 232.172 moveto
  				904.495 180.815 lineto
  				829.454 117.848 734.626 83.333 636.667 83.333 curveto
  				406.667 83.333 220 270 220 500 curveto
  				220 730 406.667 916.667 636.667 916.667 curveto
  				759.831 916.667 876.684 862.177 955.852 767.828 curveto
  				988.474 855.291 lineto
  				894.851 947.996 768.422 1000 636.667 1000 curveto
  				360.667 1000 136.667 776 136.667 500 curveto
  				136.667 224 360.667 0 636.667 0 curveto
  				753.23 0 866.13 40.725 955.852 115.135 curveto
  				closepath fill
 		 
  				869.037 541.667 moveto
  				901.025 625 lineto
  				31.989 625 lineto
  				0 541.667 lineto
  				closepath fill
 			 
  				805.059 375 moveto
  				837.048 458.333 lineto
  				31.989 458.333 lineto
  				0 375 lineto
  				closepath fill
				grestore
			} bind def
		end

		/BuildGlyph {
			1000 0
			0 0 1000 1000
			setcachedevice
			exch /CharProcs get exch
			2 copy known not {pop /.notdef} if
			get exec
		} bind def

		/BuildChar {
			1 index /Encoding get exch get
			1 index /BuildGlyph get exch
		} bind def

		currentdict
	end
	/euro-font exch definefont pop
	% End of Euro Font

	% Begin Shifted Font: copy font, rotate encoding matrix, redefine it
        20 dict begin
                old-font findfont
                {1 index /FID ne {def} {pop pop} ifelse} forall
                mark currentdict /Encoding get aload pop
                counttomark dup 165 sub roll counttomark array astore
                /Encoding exch def pop
                currentdict
        end             
        /new-shifted exch definefont pop
	% End of Shifted Shifted Font

	% Begin Composite Font
	20 dict begin
		/FontType 0 def
		/FontMatrix [1 0 0 1 0 0] def
		/FontBBox [ 0 0  0 0] def
		/WMode 0 def
		/Encoding [0 1 2] def
		old-font findfont /Encoding get dup /PrefEnc exch def pop

		/FMapType 6 def	% Use the SubsVector to figure out which one
		/FDepVector [ old-font     findfont   /euro-font  findfont
			      /new-shifted findfont ] def
		/SubsVector <00 a4 01 > readonly def
	
		currentdict
	end
	new-font exch definefont pop
	% End Composite Font
} bind def % end of adding euro to font

% if (key not defined) define key=value in current dictionary
/ifndef { % key value
	1 index where { pop pop } { def } ifelse
} def
/noheader true def
/gaudyheader false def
/def_headerpointsize 10 def
/headerpointsize def_headerpointsize ifndef
/gaudy_pointsize 15 def
/font /Courier ifndef
/def_pointsize 10 def
/pointsize def_pointsize ifndef
/Linespace {/Linespace pointsize 1.1 mul neg dup 3 1 roll def } def
/must-add-euro-to-font false ifndef
/#copies 1 ifndef
/aspectratio 1 ifndef
/formsperpage 1 ifndef
/landscape false ifndef
/magnification 1 ifndef
/margin 10 def
/orientation 0 def
/encoding /ISOLatin1Encoding def
/def_yoffset -0.30 def
/def_xoffset .35 def
/xoffset def_xoffset ifndef
/yoffset def_yoffset ifndef
/roundpage true def
/useclippath true def
/pagebbox [0 0 612 792] def

%
% function definitions
%
/inch {72 mul} bind def
/point {72 div} bind def
/min {2 copy gt {exch} if pop} bind def
/max {2 copy lt {exch} if pop} bind def
/rectpath {4 2 roll moveto dup 0 exch rlineto exch 0 rlineto neg 0 exch rlineto closepath } def

/setup {
	counttomark 2 idiv {def} repeat pop
	landscape {/orientation 90 orientation add def} if

	%
	% If user requested an encoding that does not exist, issue a warning 
	% and use whatever is available. 
	% 
	/encoding-vec
		encoding where {
			begin encoding cvx exec end	% convert name to vector
		}{
			systemdict /EncodingDirectory known {
				EncodingDirectory encoding known {	% maybe it's there
					EncodingDirectory encoding get
				}{
					/ISOLatin1Encoding where {	% find a substitute
						pop /ISOLatin1Encoding
					}{
						/StandardEncoding
					} ifelse

					%
					% Tell the user what we're substituting for what.
					% 
					(%% [ ) print
					encoding 100 string cvs print
					( not found, substituting ) print
					dup 100 string cvs print
					( ] %%\n) print

					cvx exec		% now convert name to vector
				} ifelse
			}{
				/ISOLatin1Encoding where {	% find a substitute
					pop /ISOLatin1Encoding
				}{
					/StandardEncoding
				} ifelse

				%
				% Tell the user what we're substituting for what.
				% 
				(%% [ ) print
				encoding 100 string cvs print
				( not found, substituting ) print
				dup 100 string cvs print
				( ] %%\n) print

				cvx exec		% now convert name to vector
			} ifelse
		} ifelse
	def

	systemdict /encodefont known
	{
		font findfont pointsize scalefont encoding-vec encodefont setfont
	}{
		font findfont 
		dup length dict begin
			{1 index /FID ne {def} {pop pop} ifelse} forall
			/Encoding encoding-vec def 
			currentdict
		end
		/font-newencoding exch definefont pop
		% if we need the Euro
		must-add-euro-to-font {     % add the euro and use the new font
			/font-newencoding /font-newencoding-euro add-euro-to-font
			/font-newencoding-euro
		} {			     % just use the new encoding
			/font-newencoding
		} ifelse findfont
		pointsize scalefont setfont
	} ifelse

	/charwidth (M) stringwidth pop def
	/linespace pointsize pointsize .10 mul add neg def
	/headerspace headerpointsize headerpointsize .10 mul add 2 linespace mul sub neg def 

	pagedimensions

	% if height is less than what's needed, omit top margin to avoid clipping off text from the bottom
	% if weight is less than what's needed, omit left margin to avoid clipping off text from the right

	/standard_height 66 def_pointsize def_pointsize .10 mul add mul yoffset inch sub margin 2 div add def
	/standard_gaudy_height .5 inch 64 def_pointsize def_pointsize .10 mul add mul add .1 inch add margin 2 div add def

	landscape {	%yoffset not to be changed for landscape mode because it will cut off the top part
			/rel_height standard_height def  
			gaudyheader	{ /rel_width 10.25 inch def } 
					{ /rel_width 10.0 inch def } ifelse
		}
		{	/rel_height height def 
			gaudyheader 	{ /rel_width 7.75 inch def }
					{ /rel_width 7.5 inch def } ifelse
		} ifelse

	noheader { rel_height height lt
			{ /yoffset yoffset def_yoffset sub margin point 2 div add def }if
		   width rel_width lt 
			{ /xoffset xoffset def_xoffset sub margin point 2 div sub def }if
		}
		{ gaudyheader 
			{ rel_height standard_gaudy_height lt
				{ /yoffset yoffset .1 add margin point 2 div add def }if 
			  width rel_width lt 
                        	{ /xoffset xoffset .10 sub margin point 2 div sub def }if
			}
			{ rel_height height lt
				{ /yoffset yoffset def_yoffset sub margin point 2 div add def }if 
			  width rel_width lt 
                        	{ /xoffset xoffset def_xoffset sub margin point 2 div sub def }if
			} ifelse
		} ifelse
	
	xcenter ycenter translate
	orientation neg rotate
	width 2 div neg height 2 div translate
	xoffset inch yoffset inch translate
	margin 2 div dup neg translate
	magnification dup aspectratio mul scale 

	noheader{ landscape 	{0 linespace 2 mul translate }
				{0 linespace translate } ifelse
		}
		{ gaudyheader  	{ 0 linespace translate } 
				{ landscape 	{ 0 headerspace linespace add translate }
						{ 0 headerspace translate } ifelse
				} ifelse
		} ifelse
} def

/pagedimensions {
	useclippath userdict /gotpagebbox known not and {
		/pagebbox [clippath pathbbox newpath] def
		roundpage currentdict /roundpagebbox known and {roundpagebbox} if
	} if
	pagebbox aload pop

        %
        % Adjust page dimensions to "ignore" the header.
        %
        noheader not {
	    landscape {
		exch 
		.5 sub 
		headerpointsize def_headerpointsize gt { headerpointsize def_headerpointsize sub 72 div sub } if
		exch
	    }{
		.5 sub
		headerpointsize def_headerpointsize gt { headerpointsize def_headerpointsize sub 72 div sub } if
	    } ifelse
	} if

	4 -1 roll exch 4 1 roll 4 copy
	landscape {4 2 roll} if
	sub /width exch def
	sub /height exch def
	add 2 div /xcenter exch def
	add 2 div /ycenter exch def
	userdict /gotpagebbox true put
} def


/show_date_and_time { % show the date and the time
        /Times-Roman findfont 15 scalefont setfont
	currentdict /date known
		{ 0 13 moveto date 0 setgray show } if
} def

/show_heading { % show the page heading 
	0 exch moveto
	currentdict /headerfont known 
		{headerfont findfont gaudy_pointsize scalefont setfont}
		{/Times-Roman findfont gaudy_pointsize scalefont setfont} ifelse
	dup stringwidth pop 2 div width xoffset inch .5 inch add sub 2 div exch sub
        0 rmoveto 0 setgray show
} def

/page_num_len {  % count number of digits of the current page number
	/digits 1 def
	/tens 10 def
	{	page tens lt 
			{ exit }	
			{ /digits digits 1 add def
			  /tens tens 10 mul def } ifelse
	} loop
} def	

/page_num { % convert page to a string
	page_num_len page digits string cvs 
} def

/printpage { % stk:int (right justified from this "int" value)
	page_num stringwidth pop	% get length of string
	sub 				% calculate white space 
	0 rmoveto page_num		% move over to the right that much
	0 setgray show
} def
	
/show_pagenum { % shows the page number 
	0 12 moveto 
	/Times-Roman findfont 20 scalefont setfont
	width xoffset inch .5 inch add sub printpage
} def

/show_gaudyheader {
	-20 0 width xoffset inch -.10 inch add sub .5 inch rectpath .9 setgray fill
	show_date_and_time
	currentdict /pageheading known currentdict /filename known and
		{ pageheading 22 show_heading  
		  filename 6 show_heading }
		{ currentdict /pageheading known { pageheading 14 show_heading } if 
		  currentdict /filename known { filename 14 show_heading } if 
		} ifelse
	show_pagenum
} def

/show_simpleheader {
	currentdict /headerfont known 
		{headerfont findfont headerpointsize scalefont setfont}
		{/Courier-Bold findfont headerpointsize scalefont setfont} ifelse
	currentdict /pageheading known {

		%
		% if header given then don't print page number and date and filename 
		%

                0 0 moveto
                pageheading stringwidth pop 2 div width xoffset inch .5 inch add sub 2 div exch sub
                0 rmoveto pageheading 0 setgray show } 

		%
		% else print those stuff
		%

		{ currentdict /date known
			{ 0 0 moveto date 0 setgray show } if
		currentdict /filename known 
			{ 0 0 moveto
			filename stringwidth pop 2 div width xoffset inch .5 inch add sub 2 div exch sub
			0 rmoveto filename 0 setgray show } if
		0 0 moveto width xoffset inch .75 inch add sub printpage
		} ifelse
} def

/pagesetup {
    /page exch def 
    gsave

    noheader 	{ 0 linespace neg translate }
		{ gaudyheader  	{ 0 linespace neg translate } 
				{ 0 headerpointsize .10 mul 2 linespace mul sub translate } ifelse
		} ifelse

    noheader not { gaudyheader { show_gaudyheader }
			       { show_simpleheader } 
			ifelse } 
		if

    grestore
    0 0 moveto 0

} bind def

/L {
	counttomark 2 idiv {charwidth mul currentpoint exch pop moveto show} repeat
	Linespace add dup 0 exch moveto
} bind def

/l {show Linespace add dup 0 exch moveto} bind def

/done {/lastpage where {pop lastpage} if} def
